/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：RouterMessageCreater.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.cneport.transfer.ApplicationException;
import com.cneport.transfer.constant.RouterTypeConstants;
import com.cneport.transfer.constant.StatusConstants;
import com.cneport.transfer.constant.UtilConstants;
import com.cneport.transfer.message.dtf.DtfCtrl;
import com.cneport.transfer.message.dtf.DtfCtrl.RouteList;
import com.cneport.transfer.message.dtf.DtfCtrl.RouteList.RouteInfo;
import com.cneport.transfer.router.api.ISendMessageCreater;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.model.SendMessage;
import com.cneport.transfer.router.support.service.SequenceService;
import com.cneport.transfer.util.DateUtil;
import com.cneport.transfer.util.UtilTools;
import com.cneport.transfer.util.XmlSerializer;

/**
 *@CreateDate:2012-7-11
 *@Description:路由报文生成，并生成待发送报文对象，等待后台轮询程序发送报文。
 */
public class RouterMessageCreater implements ISendMessageCreater<Router> {
    private static Logger log = Logger.getLogger(RouterMessageCreater.class);

    @Autowired
    private SequenceService sequenceService;

    public List<SendMessage> sendMessageCreator(List<Router> messageList)
            throws ApplicationException {
        Map<String, DtfCtrl> messageMap = fromObject2XMLMap(messageList);
        List<SendMessage> sendMessageList = fromXMLMap2SendMessage(messageMap);
        if (UtilTools.isNotNullAndZeroList(sendMessageList)) {
            log.info("生成待发送对象成功！共生成" + sendMessageList.size() + "条记录");
            return sendMessageList;
        }
        throw new ListIsNullOrZeroException("错误：没有生成待发送对象！");
    }

    public Map<String, DtfCtrl> fromObject2XMLMap(List<Router> messageList)
            throws ApplicationException {
        if (UtilTools.isNotNullAndZeroList(messageList)) {
            Map<String, DtfCtrl> messageMap = new HashMap<String, DtfCtrl>();
            for (Router router : messageList) {
                DtfCtrl dtfCtrl = messageMap.get(router.getStartNode());
                if (dtfCtrl != null) {
                    RouteInfo routeInfo = new RouteInfo();
                    routeInfo.setEndNode(router.getEndNode());
                    routeInfo.setNextNode(router.getRouterExpression());
                    dtfCtrl.getRouteList().getRouteInfo().add(routeInfo);
                } else {
                    dtfCtrl = new DtfCtrl();
                    dtfCtrl.setVer(UtilConstants.DTFCTRL_VER);
                    dtfCtrl.setRecNodeId(router.getStartNode());
                    RouteList routeList = new RouteList();
                    dtfCtrl.setRouteList(routeList);
                    routeList.setVersion(router.getRouterVer());
                    routeList.setEfficientTime(DateUtil
                            .convert2XMLGregorianCalendar(router
                                    .getRouterEffectTime()));
                    RouteInfo routeInfo = new RouteInfo();
                    routeInfo.setEndNode(router.getEndNode());
                    routeInfo.setNextNode(router.getRouterExpression());
                    routeList.getRouteInfo().add(routeInfo);
                    messageMap.put(dtfCtrl.getRecNodeId(), dtfCtrl);

                }
            }
            return messageMap;
        }
        throw new ListIsNullOrZeroException("错误：没有路由信息，无法生成XML报文！");
    }

    public List<SendMessage> fromXMLMap2SendMessage(
            Map<String, DtfCtrl> messageMaps) throws ApplicationException {
        Collection<DtfCtrl> values = messageMaps.values();

        List<SendMessage> sendMsgList = new ArrayList<SendMessage>();
        Date now = new Date();
        SendMessage sendMsg = null;
        for (DtfCtrl dtfCtrl : values) {
            sendMsg = new SendMessage();
            try {
                sendMsg.setContent(XmlSerializer.convert2Xml(dtfCtrl));
                log.debug(new String(sendMsg.getContent(), "UTF-8"));
            } catch (Exception e) {
                throw new ApplicationException("错误：DxpCtrl对象转换为XML错误!");
            }
            sendMsg.setCreateTime(now);
            sendMsg.setMessageType(RouterTypeConstants.ROUTE_MESSAGE);
            sendMsg.setReceiverId(dtfCtrl.getRecNodeId());
            sendMsg.setSendSeq(sequenceService.getSequence("sendSeq"));
            sendMsg.setDealTime(now);
            sendMsg.setRetFlag(StatusConstants.RETFLAG_F);
            sendMsg.setSendNum(0);
            sendMsg.setSendFlag(StatusConstants.SENDFLAG_E);
            sendMsg.setMessageId(sendMsg.getSendSeq());
            sendMsgList.add(sendMsg);
        }
        return sendMsgList;
    }

}
